package com.xiong.wuliao.mainfragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.xiong.wuliao.R;
import com.xiong.wuliao.SeekActivity;
import com.xiong.wuliao.util.FragmentHomeUtil;

/**
 * 第一个页面：首页
 */
public class HomeFragment extends Fragment implements View.OnClickListener {
    // 搜索图片按钮
    private ImageButton myButtonSeekHome;
    private TabLayout myTabLayout; // 首页上方显示的tab
    private ViewPager myViewPager; // 首页tab对应的fragment
    // ViewPager的自定义适配器
    private MyFragemntPageAdapter adapter;
    // tab名称
    private String[] tabList = {"精选","笑点低","少女心","涨知识","爱电影"};


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        findView(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        tabLayoutChange(); // 上方tab的更换
        setMyAdapter(); // 设置适配器
        setListener();  // 设置监听
    }

    private void setListener() {
        // 设置ViewPager的滑动更换Tab的监听
        myViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(myTabLayout));
        // 设置TabLayout的选择监听,与ViewPager关联
        myTabLayout.setOnTabSelectedListener(new MyTabSelectedListener());

        myButtonSeekHome.setOnClickListener(this);
    }

    private void setMyAdapter() {
        // 注意由于是Fragment中嵌套ViewPager，所以需要用getChildFragmentManager()
        adapter = new MyFragemntPageAdapter(getChildFragmentManager());
        // 设置适配器
        myViewPager.setAdapter(adapter);
        myViewPager.setOffscreenPageLimit(3);
        // 设置默认显示第一个fragment
        myViewPager.setCurrentItem(0);
        // 设置关联
        myTabLayout.setupWithViewPager(myViewPager);
        myTabLayout.setTabsFromPagerAdapter(adapter);
    }

    /**
     * 更换TabLayout,设置上方显示文字
     */
    private void tabLayoutChange() {
        myTabLayout.setTabMode(TabLayout.MODE_FIXED);
        for (int index = 0;index<tabList.length;index++){
            myTabLayout.addTab(myTabLayout.newTab().setText(tabList[index]));
        }
    }

    /**
     * 初始化控件
     */
    private void findView(View view) {
        myButtonSeekHome = (ImageButton) view.findViewById(R.id.myButtonSeekHome);
        myTabLayout = (TabLayout) view.findViewById(R.id.myTabLayoutHome);
        myViewPager = (ViewPager) view.findViewById(R.id.myViewPagerHome);
    }

    /**
     * 按钮的普通点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.myButtonSeekHome:
                Intent intent = new Intent(getActivity(), SeekActivity.class);
                startActivity(intent);
                break;
        }
    }

    /**
     * 设置TabLayout的选择监听,与ViewPager关联
     */
    private class MyTabSelectedListener implements TabLayout.OnTabSelectedListener {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            int position = tab.getPosition();
            myViewPager.setCurrentItem(position);
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    }

    /**
     * ViewPager的适配器
     */
    private class MyFragemntPageAdapter extends FragmentPagerAdapter {
        public MyFragemntPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return FragmentHomeUtil.getFragments().get(position);
        }

        @Override
        public int getCount() {
            return FragmentHomeUtil.getFragmentSize();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            // 更换tab文字
            return tabList[position];
        }
    }
}
